package gzc.xiaonei.action;

import gzc.xiaonei.model.University;
import gzc.xiaonei.service.IUniversityService;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("unchecked")
/** 处理用户注册时，选择不同的国家，省市等的大学信息的请求 */
public class changeInfoAction extends ActionSupport {

	private String proid;
	private String cid;

	@Resource
	private IUniversityService universityService;

	public String getProid() {
		return proid;
	}

	public void setProid(String proid) {
		this.proid = proid;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public IUniversityService getUniversityService() {
		return universityService;
	}

	public void setUniversityService(IUniversityService universityService) {
		this.universityService = universityService;
	}

	@Override
	public String execute() throws Exception {
//		System.out.println("proid=" + proid + "cid=" + cid);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/xml;charset=utf-8");
		//ie禁用缓存
		response.setHeader("Cache-Control", "no-cache"); 
		PrintWriter out = response.getWriter();

		final String hql = "from University where province.id=? and country.id=?";
		List<University> uniList = universityService.getResult(hql,
				new Object[] { Integer.valueOf(proid), Integer.valueOf(cid) });

//		for(int i = 0; i < uniList.size(); i++){
//			System.out.println("大学名字=" + uniList.get(i).getName());
//		}
		
		// 输出响应
		String res = "<allUniversities>";
		for (int i = 0; i < uniList.size(); i++) {
			University u = uniList.get(i);
			res += "<unis><id>" + u.getId() + "</id><name><![CDATA["
					+ u.getName() + "]]></name></unis>";
		}
		res += "</allUniversities>";
		out.write(res);
		out.close();

		return SUCCESS;
	}

}
